<?php
namespace app\member\controller;

use app\common\controller\BaseMember;
use app\common\model\User;
use app\common\model\UserInfo;

class Users extends BaseMember
{
    public function _initialize(){
        parent::_initialize();
    }

    public function index() {
		if(request()->isPost()){
			$userModel = new User();
			$data = input('post.');
			if ($data['actions'] == 'password'){    //修改密码
				$result = $userModel->validate('User.password')->allowField(true)->save($data, $data['id']);
			}elseif($data['actions'] == 'wximg'){	//修改微信图片
				$userInfoModel = new UserInfo();
				$userdata = $userInfoModel->where(['uid'=>$data['id']])->find();
				if( !empty($userdata['wx_imgurl'])){
					if ($userdata['wx_imgurl'] != '/static/common/img/logo.jpg'){      //删除之前图片
                        unlink(WEB_PATH.$userdata['wx_imgurl']);
                    }
				}
				$result = $userInfoModel->allowField(true)->save($data,['uid'=>$data['id']]);
			}else{	//修改信息
//				$data['birthday'] = strtotime($data['birthday']);
				$data['is_share'] = json_encode($data['row']);
				$result = $userModel->validate('User.edit')->allowField(true)->save($data, $data['id']);
				if($result){
					$datas = ['qq'=>$data['qq'],'weixin'=>$data['weixin'],'birthday'=>strtotime($data['birthday']),'info'=>$data['info']];
					$userModel->userInfo->save($datas,['uid'=>$data['id']]);
				}
			}
            if ($result){
                return ajaxReturn('修改成功', url('index'));
            }else{
                return ajaxReturn( $userModel->getError() );
            }

		}else{
			return $this->fetch();
		}
    }



}
